package com.hdoit.ares.web.tp.zzb.wechat;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.hdoit.ares.logical.cust.model.Customer;
import com.hdoit.ares.logical.cust.service.ISendMsgService;
import com.hdoit.ares.logical.tp.zzb.model.Tomeasure;
import com.hdoit.ares.logical.tp.zzb.service.ITomeasureService;
import com.hdoit.ares.selfcommon.enums.AresManageContainer;
import com.hdoit.ares.selfcommon.enums.tp.ComState;
import com.hdoit.ares.selfcommon.thirdparty.zzb.utils.RC4Util;
import com.hdoit.ares.selfcommon.thirdparty.zzb.utils.ZzbConfiguration;
import com.hdoit.ares.selfcommon.thirdparty.zzb.utils.ZzbManageContainer;
import com.hdoit.ares.selfcommon.utils.SsmConfiguration;
import com.hdoit.ares.selfcommon.utils.SsmManageContainer;
import com.tennetcn.common.enums.ModelStatus;
import com.tennetcn.common.message.WechatLoginModel;
import com.tennetcn.component.wechat.BaseWechat;
import com.xiaoleilu.hutool.log.dialect.log4j2.Log4j2LogFactory;

@Controller
@RequestMapping(value = "/wechat/zzb/")
public class ZzbWechat extends BaseWechat {

	@Override
	public String getRoutePath() {

		return "areas/wechat/zzb/";
	}
	
	public ISendMsgService sendMsgService;
	public ISendMsgService getSendMsgService() {
		return sendMsgService;
	}
	@Resource
	public void setSendMsgService(ISendMsgService sendMsgService) {
		this.sendMsgService = sendMsgService;
	}

	private ITomeasureService tomeasureService;
	public ITomeasureService getTomeasureService() {
		return tomeasureService;
	}
	@Resource
	public void setTomeasureService(ITomeasureService tomeasureService) {
		this.tomeasureService = tomeasureService;
	}

	private final String carInsuViewName="carInsu";
	private final String tomeasureViewName="tomeasure";
/*	private final String toFanhuaCarViewName="toFanhuaCar";*/
	private final String fanHuaFlowPathViewName="fanHuaFlowPath";
/*	private final String fanhuaTestViewName="fanhuaTest";
	private final String insuFanHuaTestViewName="insuFanHuaTest";
	private final String provinceCityViewName="provinceCity";*/
	private final String testMsgViewName="testMsg";
	private final String testViewName="test";
	
	//车险分流界面
	@RequestMapping(value = carInsuViewName, method = { RequestMethod.GET,RequestMethod.POST })
	public String carInsu(Model model){
		WechatLoginModel loginModel=wechatSession.getCurrentLogin();
		model.addAttribute("loginModel", loginModel);
		
		//众安车险参数加密传递
		Customer customer = (Customer) loginModel.get(AresManageContainer.customer_account_info);
		String key = ZzbConfiguration.getInstance().getValue(ZzbManageContainer.zhongan_openKey);
		String data = "{\"userID\":\"" + customer.getId() + "\"}";
		String sign = RC4Util.encryRC4String(data, key);
		String bizContent = "bizContent=" + sign;
		model.addAttribute("bizContent", bizContent);

		return view(carInsuViewName);
	}

	/*//泛华车险投保测算分流界面
	@RequestMapping(value = toFanhuaCarViewName, method = { RequestMethod.GET, RequestMethod.POST })
	public String toFanhuaCar(Model model) {
		WechatLoginModel loginModel = wechatSession.getCurrentLogin();
		model.addAttribute("loginModel", loginModel);

		Customer customer = (Customer) loginModel.get(AresManageContainer.customer_account_info);
		String key = ZzbConfiguration.getInstance().getValue(ZzbManageContainer.zhongan_openKey);
		String data = "{\"userID\":\"" + customer.getId() + "\"}";
		String sign = RC4Util.encryRC4String(data, key);
		String bizContent = "bizContent=" + sign;
		model.addAttribute("bizContent", bizContent);

		return view(toFanhuaCarViewName);
	}
	//泛华测试投保界面
	@RequestMapping(value = fanhuaTestViewName, method = { RequestMethod.GET, RequestMethod.POST })
	public String fanhuaTest(Model model) {
		WechatLoginModel loginModel = wechatSession.getCurrentLogin();
		model.addAttribute("loginModel", loginModel);

		Customer customer = (Customer) loginModel.get(AresManageContainer.customer_account_info);
		model.addAttribute("customer", customer);
		
		FanhuaInsuPlace fanhuaInsuPlace = new FanhuaInsuPlace();
		model.addAttribute("fanhuaInsuPlace", fanhuaInsuPlace);
		
		return view(fanhuaTestViewName);
	}
	
	//泛华测试投保确认信息界面
	@RequestMapping(value = insuFanHuaTestViewName, method = { RequestMethod.GET,RequestMethod.POST })
	public String insuFanHuaTest(Model model,TestCarCollection fanhuaCar) {
		WechatLoginModel loginModel = wechatSession.getCurrentLogin();
		model.addAttribute("loginModel", loginModel);
		Customer customer = (Customer) loginModel.get(AresManageContainer.customer_account_info);
		fanhuaCar.setBuyerIdentity(customer.getLevel());
		
		System.err.println(fanhuaCar);
		model.addAttribute("fanhuaCar", fanhuaCar);
		return view(insuFanHuaTestViewName);
	}*/
	
	//泛华投保流程界面
	@RequestMapping(value = fanHuaFlowPathViewName, method = { RequestMethod.GET,RequestMethod.POST })
	public String fanHuaFlowPath(Model model) {
		return view(fanHuaFlowPathViewName);
	}
	
	/*//城市级连
	@RequestMapping(value = provinceCityViewName, method = { RequestMethod.GET,RequestMethod.POST},produces="text/plain;charset=utf-8")
	@ResponseBody
	public String provinceCity(String keyId){
		FanhuaInsuPlaceSearch fanhuaInsuPlaceSearch=new FanhuaInsuPlaceSearch();
			
		if("all".equals(keyId)){
			fanhuaInsuPlaceSearch.setParamName("province");
		}else{
			fanhuaInsuPlaceSearch.setParentId(keyId);
			System.err.println("keyId是什么"+keyId);
		}
	    
	    List<FanhuaInsuPlace> fanhuaInsuPlaces =fanhuaInsuPlaceService.queryList(fanhuaInsuPlaceSearch);
	    	System.err.println("fanhuaInsuPlaces***"+fanhuaInsuPlaces.size());
	    return JsonUtils.toJson(fanhuaInsuPlaces);
	 }*/
	
	//编码测试
	@RequestMapping(value = testViewName, method = { RequestMethod.GET, RequestMethod.POST })
	public String test(Model model) {
		String openKey = ZzbConfiguration.getInstance().getValue(ZzbManageContainer.zhongan_openKey);
		String a = "a5f4b2b813285f595f227aca103020d7c4166d7c526eb8990398c12ca2f5300ae03aa2f18fe73796d65d0d050995ebd9d23e40a42c8561a54ad8522c74eb07d081b1899599742048c63043e498fedde82a9b710c97d81ec23c87dbff5d3071aacbeb98ae199f54e2f1996f6754dc56ae05d4ea8288767e4f95427bb4cfc9d866697e3eb60c96727183a5724dbceedbbb902e6b811edd3360f0f35d37ac38a6f11e8b555b4535fd7dc2be042102d23ef0431f71d04a79756f4aed4b0293ba5d6e6303feaccb544d071e352c70977a769604ddd164f89f3d2bc69bffa3206949b50536987d2bbf9849a35276cd517a42c7061a26cdfdbda1d407c3da33b2741fd89b310145b8c3caedf3b404bd67bbf13c0847c5c022daf9c009629a948c6c1a6614674f39a3f607b9a64f5b3eb005fac4712c48b5d65e5e19339bc9c97653d52428e7589e40d796035cc3448322f77d200ffcc32657212be873f73841831b7218edf8b5782d9543fc4e7113e524a12ec6e10a0bd34ca145a33014fe6548e96de366ac2b810050ca8b79b41a33bc3b00520220e2d5f3452ae664c294da1406f22296a789c93f98fc07c72537b1c66678c319df02beb8778765679dc00632cb3e1c6de63809dc72d2c1facf50ef8513e2e2bfd2659e3a9cce1d9183101e93c0b6ae7bce43b2d6f0bc1da2a7f24e32b65bbb257b11633cbf0553d83739fe06dfd89694a74e129890956ae890f90aa931c85dd87a09e2";
		try {
			String b1 = new String(RC4Util.decryRC4(a, openKey).getBytes("UTF-8"),"GBK") ;
			Log4j2LogFactory.get().error("bbb11111111"+b1);
			
			String b2 = new String(RC4Util.decryRC4(a, openKey).getBytes("UTF-8"),"UTF-8") ;
			Log4j2LogFactory.get().error("bbbbb2222222"+b2);
			
			String b3 = new String(RC4Util.decryRC4(a, openKey).getBytes("GBK"),"UTF-8") ;
			Log4j2LogFactory.get().error("bbbbb3333333"+b3);
			
			String b4 = new String(RC4Util.decryRC4(a, openKey).getBytes("GBK"),"GBK") ;
			Log4j2LogFactory.get().error("bbbbb4444444"+b4);
			
			
			byte [] c1= RC4Util.decryRC4(a, openKey).getBytes("UTF-8");
			Log4j2LogFactory.get().error("c1"+c1);
			
			String c2 = new String(c1,"ISO8859-1");
			Log4j2LogFactory.get().error("c2"+c2);
			
			byte[] c3 = c2.getBytes("ISO8859-1");
			Log4j2LogFactory.get().error("c3"+c3);
			
			String c4 = new String(c3,"GBK");
			Log4j2LogFactory.get().error("c4"+c4);
			
			
		/*	String c = new String(b.getBytes("UTF-8"),"ISO-8859-1");
			Log4j2LogFactory.get().error("cccccccccc"+c);

			String d = new String(c.getBytes("ISO-8859-1"), "GBK");	
			Log4j2LogFactory.get().error("ddddddddddd" + d);*/
				
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		String encoding = System.getProperty("file.encoding");
		Log4j2LogFactory.get().error(encoding);

		return view(testViewName);
	}
	
	//发送推送（报价信息）
	@RequestMapping(value = tomeasureViewName, method = { RequestMethod.GET, RequestMethod.POST })
	public String tomeasure(Model model,String id){
		System.err.println("id"+id);
		Tomeasure tomeasure = tomeasureService.queryModel(id);
		String comState = tomeasure.getComState();
		
		if("0".equals(comState)){
			tomeasure = null;
		}else {
			tomeasure.setComState(ComState.yes);
			tomeasure.setModelStatus(ModelStatus.update);
			if(!tomeasureService.applyChange(tomeasure)){
				throw new RuntimeException("update comState error");
			}
		}
		model.addAttribute("tomeasure",tomeasure);
		
		
		return view(tomeasureViewName);
	}
	
	//短信测试
	@RequestMapping(value = testMsgViewName, method = { RequestMethod.GET, RequestMethod.POST })
	public String testMsg(Model model){
		
		String phone = "15083949503";
		Integer templId = SsmConfiguration.getInstance().getValueInt(SsmManageContainer.cesuanTemplId);
		System.err.println("templId*****"+templId);
		String ipAddress ="192.168.200.186";
		ArrayList<String> params = new ArrayList<String>();
		params.add("曹承萌");
		params.add("3516252");
		
		if(!sendMsgService.sendVerifyNoParams(phone, templId, ipAddress, params)){
			throw new RuntimeException("*******");
		}
		/*String phone = "15083949503";
		Integer templId = 85221;
		ArrayList<String> params = new ArrayList<String>();
		QCloudConfigModel qcm = QCloudConfiguration.getInstance().qcloudConfigModel;
		try {
			SmsSingleSender sender = new SmsSingleSender(qcm.getSmsAppId(), qcm.getSmsAppKey());
			SmsSingleSenderResult result = sender.sendWithParam("86", phone, templId, params, "", "", "");
			System.err.println("result+++"+result);
		} catch (Exception ex) {
			ex.printStackTrace();
		}*/
		/*try {
			Integer appId = 1400038100;
			String appKey = "b7f8984aaa9887f6c6c0e378b9d87068";
			String nationCode = "86";
			String phoneNumber = "15083949503";
			int templId = 85221;
			ArrayList<String> params = new ArrayList<String>();
			String sign = "";
			String extend = "";
			String ext = "";
		    SmsSingleSender msender = new SmsSingleSender(appId,appKey);
		    SmsSingleSenderResult result = msender.sendWithParam(nationCode, phoneNumber, templId, params, sign, extend, ext);
		    System.out.print(result);
		    
		} catch (HTTPException e) {
		    // HTTP响应码错误
		    e.printStackTrace();
		} catch (JSONException e) {
		    // json解析错误
		    e.printStackTrace();
		} catch (IOException e) {
		    // 网络IO错误
		    e.printStackTrace();
		}*/
		return view(testMsgViewName);
	}
}
